package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallCompleteForManagerView;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.presenter.AddCompletionOrderCommentPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallCompleteForManagerPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.activity.AddCompletionOfEvaluationActivity;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.AccessibleObject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/23.
 */

public class EngineeringInstallCompletionOrderForManagerActivity extends Activity implements View.OnClickListener, IEngineeringInstallOrderDetailView, IEngineeringInstallCompleteForManagerView {
    private TextView tv_title;
    private ViewGroup rl_back;
    private final String TAG = EngineeringInstallCompletionOrderForManagerActivity.class.getSimpleName();
    private TextView tv_project_name, tv_engineering_name;
    private View rl_project;
    private LinearLayout ll_engineering_info_content;
    private TextView edt_dispath_order_time_colon, edt_completionorder_date, edt_install_company;
    private TextView tv_add_evaluation;
    private ImageView img_project_select;
    private TextView tv_construction_quality, tv_onstruction_schedule, tv_construction_environment, tv_construction_safe, tv_final_evaluation;
    private View rl_evalutaion;
    private TextView btn_confirm_evaluation;
    private String quality, schedule, environment, safe, final_evaluation;
    private OrderParentBean orderParentBean;
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private EngineeringInstallOrderDetailPresenter engineeringInstallOrderDetailPresenter;
    private EngineeringInstallCompleteForManagerPresenter engineeringInstallCompleteForManagerPresenter;

    private GridView gridview_completion, gridview_texie_photo, gridview__other_photo;
    private GridAdapter gridview_completiondapter, gridview_texie_photoadapter, gridview__other_photoadapter;

    private List<String> serverimgUrlListList = new ArrayList<>();
    private List<String> serverortherimgUrlListList = new ArrayList<>();
    private List<String> servertexieimgUrlListList = new ArrayList<>();
    private boolean isClick;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        setContentView(R.layout.layout_engineeringinstall_completionorder_for_manager);

        engineeringInstallOrderDetailPresenter = new EngineeringInstallOrderDetailPresenter(this);
        engineeringInstallCompleteForManagerPresenter = new EngineeringInstallCompleteForManagerPresenter(this);

        init();
        engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();


    }

    private void init() {
        tv_title = findViewById(R.id.tv_title);
        rl_back = findViewById(R.id.rl_back);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        rl_project = findViewById(R.id.rl_project);
        ll_engineering_info_content = findViewById(R.id.ll_engineering_info_content);
        edt_dispath_order_time_colon = findViewById(R.id.edt_dispath_order_time_colon);
        edt_completionorder_date = findViewById(R.id.edt_completionorder_date);
        edt_install_company = findViewById(R.id.edt_install_company);


        img_project_select = findViewById(R.id.img_project_select);
        tv_add_evaluation=findViewById(R.id.tv_add_evaluation);
        btn_confirm_evaluation = findViewById(R.id.btn_confirm_evaluation);
        tv_construction_quality = findViewById(R.id.tv_construction_quality);
        tv_onstruction_schedule = findViewById(R.id.tv_onstruction_schedule);
        tv_construction_environment = findViewById(R.id.tv_construction_environment);
        tv_construction_safe = findViewById(R.id.tv_construction_safe);
        tv_final_evaluation = findViewById(R.id.tv_final_evaluation);
        rl_evalutaion = findViewById(R.id.rl_evalutaion);


        gridview_completion = findViewById(R.id.gridview_completion);
        gridview_texie_photo = findViewById(R.id.gridview_texie_photo);
        gridview__other_photo = findViewById(R.id.gridview__other_photo);

        gridview_completion.setNumColumns(3);
        gridview_texie_photo.setNumColumns(3);
        gridview__other_photo.setNumColumns(3);

        gridview_completiondapter = new GridAdapter(this);
        gridview_texie_photoadapter = new GridAdapter(this);
        gridview__other_photoadapter = new GridAdapter(this);

        gridview_completiondapter.setData(serverimgUrlListList);
        gridview_texie_photoadapter.setData(servertexieimgUrlListList);
        gridview__other_photoadapter.setData(serverortherimgUrlListList);

        gridview_completion.setAdapter(gridview_completiondapter);
        gridview_texie_photo.setAdapter(gridview_texie_photoadapter);
        gridview__other_photo.setAdapter(gridview__other_photoadapter);

        tv_title.setText("工程安装");
        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        rl_project.setOnClickListener(this);
        tv_add_evaluation.setOnClickListener(this);
        btn_confirm_evaluation.setOnClickListener(this);
        rl_back.setOnClickListener(this);


        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
        }

    }

    private void initData() {
        if (null != engineeringInstallationOrderBean) {
            tv_project_name.setText(engineeringInstallationOrderBean.getProjectName());
            tv_engineering_name.setText("/" + engineeringInstallationOrderBean.getEngineeringName());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getBeginDate()));
            edt_completionorder_date.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getFinishDate()));
            edt_install_company.setText(engineeringInstallationOrderBean.getReceiveAccountId());


            CommonEnum.CompleteStatus completeStatus = engineeringInstallationOrderBean.getCompleteStatus();

            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClickable(true);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClickable(false);
                }
            }


            List<String> completeImageList = engineeringInstallationOrderBean.getCompleteImageList();
            if (null != completeImageList) {
                gridview_completiondapter.setData(completeImageList);
                gridview_completiondapter.notifyDataSetChanged();
//                    serverimgUrlListList.clear();
//                    serverimgUrlListList.addAll(completedImgUrlList);
            }
            List<String> completeCloseUpImageList = engineeringInstallationOrderBean.getCompleteCloseUpImageList();
            if (null != completeCloseUpImageList) {
                gridview_texie_photoadapter.setData(completeCloseUpImageList);
                gridview_texie_photoadapter.notifyDataSetChanged();
            }

            List<String> completeOtherImageList = engineeringInstallationOrderBean.getCompleteOtherImageList();
            if (null != completeOtherImageList) {
                gridview__other_photoadapter.setData(completeOtherImageList);
                gridview__other_photoadapter.notifyDataSetChanged();
            }

        }


    }

    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void btnClickable(boolean bool) {
        isClick = bool;
        if (bool) {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }

    private void submitCompletion() {
        engineeringInstallCompleteForManagerPresenter.engineeringInstallCompleteForManager();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
            case R.id.rl_project:
                showEngineeringInfoDialog();
                break;
                // 评价
            case R.id.tv_add_evaluation:
                mIntent = new Intent(EngineeringInstallCompletionOrderForManagerActivity.this, AddCompletionOfEvaluationActivity.class);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            case R.id.btn_confirm_evaluation:
                if (isClick)
                    submitCompletion();
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    Bundle mBundle = data.getExtras();
                    if (null != mBundle) {

                        quality = mBundle.getString("quality");
                        schedule = mBundle.getString("schedule");
                        environment = mBundle.getString("environment");
                        safe = mBundle.getString("safe");
                        final_evaluation = mBundle.getString("final_evaluation");

                        tv_construction_quality.setText(quality);
                        tv_onstruction_schedule.setText(schedule);
                        tv_construction_environment.setText(environment);
                        tv_construction_safe.setText(safe);
                        tv_final_evaluation.setText(final_evaluation);

                        rl_evalutaion.setVisibility(View.VISIBLE);


                    }

                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
//
//    @Override
//    public String getEvaluateInfo() {
//        JSONObject jsonObj = new JSONObject();
//        try {
//            jsonObj.put("施工质量", quality);
//            jsonObj.put("施工进度", schedule);
//            jsonObj.put("施工环境", environment);
//            jsonObj.put("施工安全", safe);
//            jsonObj.put("综合评价", final_evaluation);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return jsonObj.toString();
//    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public long getFeedbackDate() {
        return 0;
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public void engineeringInstallCompleteForManagerSuccessed() {
        Toast.makeText(this, "竣工提交成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void engineeringInstallCompleteForManagerFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();

    }


    @Override
    public void getEngineeringInstallOrderDetailSuccessed(EngineeringInstallationOrderBean orderBean) {
        engineeringInstallationOrderBean = orderBean;
        initData();
    }

    @Override
    public void getEngineeringInstallOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
